package cn.dnui_lxx432.dnuifood_16110100432.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.CollectStatus;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.controller.FoodInfoActivity;
import cn.dnui_lxx432.dnuifood_16110100432.controller.FoodListActivity;
import cn.dnui_lxx432.dnuifood_16110100432.model.CollectModel;
import cn.dnui_lxx432.dnuifood_16110100432.model.UserModel;

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<CollectStatus> mDataList;
    private LayoutInflater mLayoutInflater;

    public CollectAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.collect_adapter,parent,false);

        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, final int position) {
        final CollectStatus entity = (CollectStatus)mDataList.get(position);
        if (null == entity)
            return;
        CollectAdapter.ViewHolder holder = (CollectAdapter.ViewHolder) holder1;
        if (entity.getFlag().equals("0")){
            holder.collect_name.setText(entity.getShopname());
            holder.collect_content.setText(entity.getAddress());
            if (!entity.getPic().equals("")){
                Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);
            }
            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到店铺详细页
                    String str = String.valueOf(mDataList.get(position).getShop_id());
                    Intent intent = new Intent(mContext,FoodListActivity.class);
                    intent.putExtra("shop_id",str);
                    mContext.startActivity(intent);
                }
            });
            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //取消收藏
                    String user_id = String.valueOf(mDataList.get(position).getUser_id());
                    String shop_id = String.valueOf(mDataList.get(position).getShop_id());
                    uncollectShop(user_id,shop_id,mDataList);
                    mDataList.remove(position);
                    notifyDataSetChanged();
                }
            });

        }else {
            // holder.collect_foodid.setText(entity.getFood_id());
            holder.collect_name.setText(entity.getFoodname());
            holder.collect_content.setText(entity.getPrice()+"元");
            if (!entity.getPic().equals("")){
                Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);
            }
            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到菜谱详细页
                    String str1 = String.valueOf(mDataList.get(position).getShop_id());
                    Intent intent = new Intent(mContext, FoodInfoActivity.class);
                    intent.putExtra("food_id",str1);
                    mContext.startActivity(intent);
                }
            });
            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //取消收藏
                    String user_id = String.valueOf(mDataList.get(position).getUser_id());
                    String food_id = String.valueOf(mDataList.get(position).getFood_id());
                    uncollectFood(user_id,food_id,mDataList);
                    mDataList.remove(position);
                    notifyDataSetChanged();
                }
            });

        }

    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView collect_pic;
        private TextView collect_name,collect_content,collect_shopid,collect_foodid;
        private Button collect_enter, collect_cancle;
        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic=(ImageView)itemView.findViewById(R.id.collectfragment_pic);
            collect_name=(TextView) itemView.findViewById(R.id.collectfragment_name);
            collect_content=(TextView) itemView.findViewById(R.id.collectfragment_content);
            collect_foodid=(TextView) itemView.findViewById(R.id.collectfragment_foodid);
            collect_shopid=(TextView)itemView.findViewById(R.id.collectfragment_shopid);
            collect_enter=(Button) itemView.findViewById(R.id.collect_enter);
            collect_cancle=(Button) itemView.findViewById(R.id.collect_cancle);

        }
    }


    @Override
    public int getItemCount() {
        if (mDataList==null)
            return 0;
        else
            return mDataList.size();
    }


    private void uncollectShop(String user_id, String shop_id, List<CollectStatus> mDataList){
        //取消收藏店铺
        CollectModel collectModel = new CollectModel();
        collectModel.unCollectShop(user_id,shop_id,mDataList);
    }

    private void uncollectFood(String user_id, String food_id, List<CollectStatus> mDataList){
        //取消收藏菜谱
        CollectModel collectModel = new CollectModel();
        collectModel.unCollectFood(user_id,food_id,mDataList);

    }

}

